package cn.edu.zafu.bigdata.zengyadian;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {
    public static void main(String[] args) {
        try {
            // 创建UDP套接字
            DatagramSocket clientSocket = new DatagramSocket();

            // 指定服务端的地址（这里需要替换成同学电脑的IP地址）和端口（假设服务端监听端口为8888）
            InetAddress serverAddress = InetAddress.getByName("xxx的服务器IP地址");
            int serverPort = 8888;

            // 发送请求数据包给服务端（这里发送一个空数据包作为请求）
            byte[] requestData = new byte[0];
            DatagramPacket requestPacket = new DatagramPacket(requestData, requestData.length, serverAddress, serverPort);
            clientSocket.send(requestPacket);

            // 准备接收服务端返回的时间数据包
            byte[] buffer = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
            clientSocket.receive(receivePacket);

            // 解析并打印接收到的服务器时间
            String receivedTime = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("从服务端获取的当前时间：" + receivedTime);

            // 关闭客户端套接字
            clientSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}